package com.system.honour.controller;

import com.system.honour.beans.PageQuery;
import com.system.honour.beans.PageResult;
import com.system.honour.common.ResultDTO;
import com.system.honour.dto.UserDTO;
import com.system.honour.entity.SysUser;
import com.system.honour.exception.CustomizeErrorCode;
import com.system.honour.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/sys/user")
@Api(tags = "召唤师信息管理")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据召唤师名称查询召唤师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "召唤师名", required = true, paramType = "query",dataType = "String")
    })
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public ResultDTO user(String username){
        log.info("UserController.user():param ( username ="+username+")");
        return ResultDTO.okOf(userService.getUser(username));
    }

    @ApiOperation(value = "分页查询召唤师列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",required = true,paramType = "query",dataType ="int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示数量",required = true,paramType = "query",dataType ="int",defaultValue = "10")
    })
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResultDTO page(Integer pageNo,Integer pageSize){
        PageQuery page = new PageQuery();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        PageResult result = userService.getUserByPage(page);
        result.setPageQuery(page);
        return ResultDTO.okOf(result.getTotal(),result.getData(),result.getPageQuery());
    }

    @ApiOperation(value = "添加召唤师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('sys$user$save')")
    public ResultDTO saveUser(@RequestBody UserDTO param, Integer roleId){

        ResultDTO resultDTO = userService.save(param, roleId);
        return resultDTO;
    }

    @ApiOperation(value = "编辑召唤师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updateUser(@RequestBody UserDTO param, Integer roleId){
        SysUser user = null;
        user = userService.getByTelephone(param.getTelephone());
        if (user!=null && !(user.getId().equals(param.getId()))){
            return ResultDTO.errorOf(CustomizeErrorCode.INCORRECT_INFORMATION);
        }
        if (roleId==null){
            return ResultDTO.errorOf(CustomizeErrorCode.NO_ROLE_ID);
        }
        ResultDTO resultDTO = userService.update(param,roleId);
        return resultDTO;
    }
    @ApiOperation(value = "删除召唤师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "召唤师id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ResultDTO deleteUser(Integer id){
        ResultDTO resultDTO = userService.deleteById(id);
        return resultDTO;
    }

    @ApiOperation(value = "召唤师名分页模糊搜索召唤师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",required = true,paramType = "query",dataType ="int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示数量",required = true,paramType = "query",dataType ="int",defaultValue = "10"),
            @ApiImplicitParam(name = "username",value = "召唤师名",paramType = "query",dataType ="String")
    })
    @RequestMapping(value = "/vague",method = RequestMethod.POST)
    public ResultDTO getUserByVague(String username,Integer pageNo,Integer pageSize){
        PageQuery page = new PageQuery();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        PageResult result = userService.getUserByVague(username,page);
        result.setPageQuery(page);
        return ResultDTO.okOf(result.getTotal(),result.getData(),result.getPageQuery());
    }
}
